#loader crafttweaker reloadableevents
import crafttweaker.events.IEventManager;

import crafttweaker.block.IBlockDefinition;
import crafttweaker.block.IBlockState;
import crafttweaker.block.IBlock;

import crafttweaker.world.IFacing;
import crafttweaker.util.Position3f;
import crafttweaker.world.IBlockPos;

import crafttweaker.world.IWorld;
import crafttweaker.data.IData;

import crafttweaker.server.IServer;
import crafttweaker.command.ICommandSender;

import crafttweaker.event.WorldTickEvent;
import crafttweaker.event.BlockNeighborNotifyEvent;

import crafttweaker.item.IItemStack;
import crafttweaker.item.IIngredient;
import crafttweaker.entity.IEntityItem;


global ALLIBlockFacing as IFacing[]=[IFacing.north(),IFacing.east(),IFacing.south(),
    IFacing.west(),IFacing.down(),IFacing.up()];
function checkStrongPower(w as IWorld,p as IBlockPos) as bool{
    for i in ALLIBlockFacing{
        if(w.getStrongPower(p.getOffset(i,1),i)>0)return true;
    }
    return false;
}
function checkRSWork(w as IWorld,p as IBlockPos) as bool{
    if(checkStrongPower(w,p))return true;
    for i in ALLIBlockFacing{
        if(checkStrongPower(w,p.getOffset(i,1)))return true;
    }
    return false;
}

function match_by_meta(a as IBlockState, b as IBlockState, c as int) as bool{
    var ret as bool=false;
    if(isNull(b))return true;
    if(a.block.definition.id==b.block.definition.id){
        if(a.meta==c | c<0){
            ret=true;
        }
    }
    return ret;
}
function checkBlockStates(w as IWorld, p as IBlockPos, x_shift as int, y_shift as int, z_shift as int, MultB as IBlockState[][][], MultBMeta as int[][][]) as bool{
    for i in 0 to MultB.length for j in 0 to MultB[i].length for k in 0 to MultB[i][j].length{
        if(isNull(MultB[i][j][k]))continue;
        if(!(match_by_meta(w.getBlockState(Position3f.create(
                p.getX()+x_shift+i,
                p.getY()+y_shift+j,
                p.getZ()+z_shift+k).asBlockPos()),
            MultB[i][j][k],MultBMeta[i][j][k])))return false;
    }
    return true;
}

//scripts.Altars.MusicAltarv2Recipe

function checkMuaStructure(w as IWorld, p as IBlockPos)as bool{
        var pl as IBlockState=<blockstate:botania:pylon>;
        var mx as IBlockState=<blockstate:avaritia:block_resource>;
        var muac as IBlockState=<blockstate:minecraft:repeating_command_block>;
        var trpl as IBlockState=<blockstate:botania:terraplate>;
        var z0 as IBlockState[]=[null,null,null,null,null];
        var z1 as IBlockState[]=[null,null,pl,null,null];
        var z2 as IBlockState[]=[null,null,mx,null,null];
        var z3 as IBlockState[]=[pl,mx,muac,mx,pl];
        var z4 as IBlockState[]=[null,null,trpl,null,null];
        /**/var mz0 as int[]=[-1,-1,-1,-1,-1];
        var mz1 as int[]=[-1,-1,2,-1,-1];
        var mz2 as int[]=[-1,2,-1,2,-1];/*/
        var mz0 as int[]=[-1,-1,-1,-1,-1];
        var mz1 as int[]=[-1,-1,-1,-1,-1];
        var mz2 as int[]=[-1,-1,-1,-1,-1];/**/
        return checkBlockStates(w,p,-2,-1,-2,
            [[z0,z1,z0],[z0,z2,z0],[z2,z3,z4],[z0,z2,z0],[z0,z1,z0]],
            [[mz0,mz0,mz0],[mz0,mz1,mz0],[mz1,mz2,mz0],[mz0,mz1,mz0],[mz0,mz0,mz0]]);
}

function convert_mua_info_to_string(p as IBlockPos, r as int, i as int){
    pr
}

//MusicAltarChecking
events.onBlockNeighborNotify(function(event as BlockNeighborNotifyEvent){
     var w as IWorld=event.world;
    var pos0 as IBlockPos = event.position;
    
    var poses=[pos0] as IBlockPos[];
    for tempv0 in event.notifiedSides{
        poses+=pos0.getOffset(tempv0,1);
    }

    for p in poses{
        if(isNull(w.getBlock(p)))continue;
        if(w.isAirBlock(p))continue;
        if(isNull(w.getBlock(p).data))continue;
        if(!(w.getBlock(p).data has "powered"))continue;
        if(w.getBlock(p).data.powered > 0 as byte)continue;
        if(!checkRSWork(w,p))continue;
        //checkStructure
        if(!checkMuaStructure(w,p))continue;

        server.commandManager.executeCommand(server,"say Music altar is stimmulated");
        //regist into the chunk
        if((w.getBlock(p).data has "asMua"))continue;
        server.commandManager.executeCommand(server,"say a");
        
        var dat as IData=w.getCustomChunkData(p);
        if(!(dat has "workingMua")){
            w.setCustomChunkData({workingMua:[]},p);
            dat=w.getCustomChunkData(p);
        }
        w.updateCustomChunkData({workingMua: {
            x:xL,y:yL,z:zL
        }},p);
        /**
        val pp as IBlockPos=p.getOffset(IFacing.up(),1);
        w.setBlockState(w.getBlockState(pp),w.getBlock(pp).data+{"Mana":1919810}as IData,pp);
        server.commandManager.executeCommand(server,"say "+(w.getBlock(pp).data+{"Mana":1919810}as IData).asString());/**/
    }
});
//NoteBlock
/*
events.onBlockNeighborNotify(function(event as BlockNeighborNotifyEvent){
    var w as IWorld=event.world;
    var pos0 as IBlockPos = event.position;
    
    var poses=[pos0] as IBlockPos[];
    for tempv0 in event.notifiedSides{
        poses+=pos0.getOffset(tempv0,1);
    }

    for p in poses{
        if(isNull(w.getBlock(p)))continue;
        if(w.isAirBlock(p))continue;
        if(isNull(w.getBlock(p).data))continue;
        var bDat as IData=w.getBlock(p).data;
        if(!(bDat has "powered"))continue;
        if(bDat.powered > 0 as byte)continue;
        if(!checkRSWork(w,p))continue;
        if(w.getBlock(p).definition.id!="minecraft:noteblock")continue;

        var dat as IData=w.getCustomChunkData(p);
        if(!(dat has "workingMua"))continue;
        if((dat.workingMua.x as int[]).length<2)continue;

        //Check if_ all muas are right
        var xL as int[]=[-1] as int[];
        var yL as int[]=[-1] as int[];
        var zL as int[]=[-1] as int[];
        for i in 1 to (dat.workingMua.x as int[]).length{
            var p_mua as IBlockPos=Position3f.create(
                (dat.workingMua.x as int[])[i],
                (dat.workingMua.y as int[])[i],
                (dat.workingMua.z as int[])[i]
            ) as IBlockPos;
            if(w.getBlock(p).definition.id!="minecraft:repeating_command_block"){
                continue;
            }
            if(!checkMuaStructure(w,p_mua) || !checkRSWork(w,p_mua)){
                //w.getBlock(p).data.working="false";
                continue;
            }
        }

    }
});*/